package com.samebuilding.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.samebuilding.dao.UserVisitDAO;
import com.samebuilding.domain.UserVisit;

public class UserVisitDAOImpl extends GenericDAOImpl<UserVisit, Integer>
		implements UserVisitDAO {

	private static final String DELETE_BY_UID_AND_VISITUID = "delete from UserVisit where visitUserNo = ? and visitedUserNo = ?";
	
	private static final String GET_I_VISIT_WHO = "from UserVisit uv where uv.visitUserNo = ?";
	private static final String GET_COUNT_I_VISIT_WHO = "select count(*) from UserVisit uv where uv.visitUserNo = ?";
	private static final String GET_WHO_VISIT_I = "from UserVisit uv where uv.visitedUserNo = ?";
	private static final String GET_COUNT_WHO_VISIT_I = "select count(*) from UserVisit uv where uv.visitedUserNo = ?";
	
	
	public void deleteByUidAndVisitUid(final Integer visitUserNo,
			final Integer visitedUserNo) {
		getHibernateTemplate().execute(new HibernateCallback() {
			public Object doInHibernate(Session s) throws HibernateException, SQLException {
				Query query = s.createQuery(DELETE_BY_UID_AND_VISITUID);
		        query.setInteger(0, visitUserNo);
		        query.setInteger(1, visitedUserNo);
		        query.executeUpdate();
		        return null;
			}
		});

	}

	public int getCountIvisitWho(final Integer visitUserNo) {
		long count = (Long)this.getHibernateTemplate().execute(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(GET_COUNT_I_VISIT_WHO);
				query.setInteger(0, visitUserNo);

				return query.uniqueResult();
			}
		});
		
		return (int)count;
	}

	public int getCountWhoVisitI(final Integer visitedUserNo) {
		long count = (Long)this.getHibernateTemplate().execute(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(GET_COUNT_WHO_VISIT_I);
				query.setInteger(0, visitedUserNo);

				return query.uniqueResult();
			}
		});
		
		return (int)count;
	}

	@SuppressWarnings("unchecked")
	public List<UserVisit> getIVisitWho(final Integer visitUserNo, final int start,
			final int limit) {
		return (List<UserVisit>)getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(GET_I_VISIT_WHO);
				query.setInteger(0, visitUserNo);
				query.setFirstResult(start);
				query.setMaxResults(limit);
				List<UserVisit> list = query.list();
				return list;
			}
		});
		
	}

	@SuppressWarnings("unchecked")
	public List<UserVisit> getWhoVisitI(final Integer visitedUserNo, final int start,
			final int limit) {
		return (List<UserVisit>)getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(GET_WHO_VISIT_I);
				query.setInteger(0, visitedUserNo);
				query.setFirstResult(start);
				query.setMaxResults(limit);
				List<UserVisit> list = query.list();
				return list;
			}
		});
	}

}
